
const {addTemplate,getAllTemplate,getAllTemplateByID,deleteByID,uploadexcel,
      getexcelfiles,delexcelByID,getexcelsListCount} =require('../db/template')
const { result } =require('../config/result')

class TemplateController{
  
  async delexcelByID(ctx){
    const {id} = ctx.request.body
    //console.log(flag,id)
    const ret=await delexcelByID(id)
    //console.log(ret)
    result.success=true;
    result.message='删除成功'
    result.token=''
    result.total=0
    result.data=ret
    ctx.body=result
  }
  async getexcelfiles(ctx){
    const retcount=await getexcelsListCount(ctx.request.query)
    //console.log(retcount)//[ { total: 25 } ]
    let total = retcount[0].total
    console.log(total)
    const ret=await getexcelfiles(ctx.request.body)
    //console.log(ret)
    result.success=true;
    result.message='获取成功'
    result.token=''
    result.total=total
    result.data=ret
    ctx.body=result
  }

  async uploadExcel(ctx){
      const file = ctx.request.files?.file;
      //console.log('ctx.request:',ctx.request.body)
      if(file)
      {
        //将上传文件信息存入数据库
        try{
          const data= ctx.request.body  
          const {id}=data  
          const url=`${ctx.origin}/upload/excelData/${file.newFilename}`
          const oldFileName=file.originalFilename
          const newFileName=file.newFilename
          const savepath=file.filepath      
          const ret =  await uploadexcel(data,savepath,url,newFileName,oldFileName) 
          let newID=0
          if(id <1){
            newID = ret[0] //新增后的返回ID值
          }
          else{
            newID = id
          }  
          //console.log(result)
          //ctx.body = result
        //path.join(__dirname, "/public/upload/excelData")
        //要将文件存储信息存到数据库
        result.success=true;
        result.message='上传成功'
        result.token=''
        result.total=0
        result.data={
          id:newID,
          url:url,
          oldFileName:oldFileName,
          newFileName:newFileName,
          savepath:savepath
        }
        }catch(e){
          ctx.app.emit('error',e.message,ctx)  
        }      
      }   
      else{
        result.success=false;
        result.message='上传失败'
        result.token=''
        result.total=0
        result.data=null
      }
      //console.log(result)
      ctx.body=result;  
  }
  async delByID(ctx){
    try{
      const {id}=ctx.request.params
      const ret =  await deleteByID(id)      
      result.success = true 
      result.message = "模版删除成功"
      result.data = ret 
      ctx.body = result
    }catch(e){
        ctx.app.emit('error',e.message,ctx)  
    } 
  }

  async getALLTpByID(ctx){
    try{
      const {id}=ctx.request.params
      const ret =  await getAllTemplateByID(id)      
      result.success = true 
      result.message = "模版获取成功"
      result.data = ret 
      ctx.body = result
    }catch(e){
        ctx.app.emit('error',e.message,ctx)  
    } 
  }

  async getALLTp(ctx){
    try{
      const ret =  await getAllTemplate()      
      result.success = true 
      result.message = "模版获取成功"
      result.data = ret 
      ctx.body = result
    }catch(e){
        ctx.app.emit('error',e.message,ctx)  
    } 
  }
  async saveTemplate(ctx){
    try{
      const templatedata= ctx.request.body     
      const ret =  await addTemplate(templatedata)    
      if(ret && ret.length>0){
        result.success = true 
        result.message = "模版保存成功"
        result.data = ret 
      }
      else{
        result.success = false
        result.message = '模版保存失败'
        result.data = null
      }  
      console.log(result)
      ctx.body = result
    }catch(e){
        ctx.app.emit('error',e.message,ctx)  
    } 
  }
}

module.exports= new TemplateController()